package com.she.iiicoursecenter.view.tab2;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_CourseSelectedList;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_MainActivity;
import com.she.iiicoursecenter.bean.CourseSelectedListBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.connect.bean.SearchConditionedBean;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab2_MainView extends CustomizedView implements OnClickListener {

	private Spinner spinner1;
	private Spinner spinner2;
	private Spinner spinner3;
	private String date,slot,time;
	private SearchConditionedBean searchConditionedBean;
	private List<String> DateList,SlotList,TimeList;
	ArrayAdapter<String> dateAdapter,slotAdapter,timeAdapter;
	
	private ImageView tab2_button;

	public Tab2_MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		try {
			searchConditionedBean = (SearchConditionedBean) getApp().getSearchConditionedData().clone();
			DateList = new ArrayList<String>();
			SlotList = new ArrayList<String>();
			TimeList = new ArrayList<String>();
			for (int i = 0; i < searchConditionedBean.getDateList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getDateList().get(i)).getString("name");
				DateList.add(temp);
			}
			for (int i = 0; i < searchConditionedBean.getSlotList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getSlotList().get(i)).getString("name");
				
//				temp = temp.replaceAll(",", "");
//				temp = temp.replaceAll("1", "一");
//				temp = temp.replaceAll("2", "二");
//				temp = temp.replaceAll("3", "三");
//				temp = temp.replaceAll("4", "四");
//				temp = temp.replaceAll("5", "五");
//				temp = temp.replaceAll("6", "六");
//				temp = temp.replaceAll("7", "日");
//				if (temp.equals("全部")) {
//					SlotList.add(temp.toUpperCase());
//				}else{
					SlotList.add(temp);
//				}
			}
			for (int i = 0; i < searchConditionedBean.getTimeList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getTimeList().get(i)).getString("name");
				TimeList.add(temp);
			}
		} catch (CloneNotSupportedException e) {
			DateList = getApp().getSearchConditionedData().getDateList();
			SlotList = getApp().getSearchConditionedData().getSlotList();
			TimeList = getApp().getSearchConditionedData().getTimeList();
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		//
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab2_main, this, true);
		//
		//日期
		dateAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, DateList);
		dateAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		//時段
		slotAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, SlotList);
		slotAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		//時間
		timeAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, TimeList);
		timeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		spinner1 = (Spinner) this.findViewById(R.id.tab2_sp1);
		spinner1.setAdapter(dateAdapter);
		spinner1.setBackgroundDrawable(null);
		spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					date = new JSONObject(getApp().getSearchConditionedData().getDateList().get(arg2)).getString("id");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		spinner2 = (Spinner) this.findViewById(R.id.tab2_sp2);
		spinner2.setAdapter(slotAdapter);
		spinner2.setBackgroundDrawable(null);
		spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					slot = new JSONObject(getApp().getSearchConditionedData().getSlotList().get(arg2)).getString("id");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		spinner3 = (Spinner) this.findViewById(R.id.tab2_sp3);
		spinner3.setAdapter(timeAdapter);
		spinner3.setBackgroundDrawable(null);
		spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				try {
					time = new JSONObject(getApp().getSearchConditionedData().getTimeList().get(arg2)).getString("id");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		tab2_button = (ImageView) this.findViewById(R.id.tab2_button);
		tab2_button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				getApp().fadein = true;
				Message msg = Tab2_MainView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what = Tab2_MainView.this.getApp().MSG_WHAT_CHANGEVIEW;
				CourseSelectedListBundleBean bundleBean = new CourseSelectedListBundleBean(date, slot, time);
				ViewChageDataBean dataBean = new ViewChageDataBean(Tab2_MainActivity.class, Tab2_CourseSelectedList.class, bundleBean);
				msg.obj = dataBean;
				Tab2_MainView.this.getApp().mChangeViewHandler.sendMessage(msg);
				Tab2_MainView.this.getApp().tab2Date = date;
				Tab2_MainView.this.getApp().tab2Slot = slot;
				Tab2_MainView.this.getApp().tab2Time = time;
			}
		});

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
	
	public void notifyAdapter(){
		
		try {
			searchConditionedBean = (SearchConditionedBean) getApp().getSearchConditionedData().clone();
			DateList = new ArrayList<String>();
			SlotList = new ArrayList<String>();
			TimeList = new ArrayList<String>();
			for (int i = 0; i < searchConditionedBean.getDateList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getDateList().get(i)).getString("name");
				DateList.add(temp);
			}
			for (int i = 0; i < searchConditionedBean.getSlotList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getSlotList().get(i)).getString("name");
					SlotList.add(temp);
			}
			for (int i = 0; i < searchConditionedBean.getTimeList().size(); i++) {
				String temp = new JSONObject(searchConditionedBean.getTimeList().get(i)).getString("name");
				TimeList.add(temp);
			}
		} catch (CloneNotSupportedException e) {
			DateList = getApp().getSearchConditionedData().getDateList();
			SlotList = getApp().getSearchConditionedData().getSlotList();
			TimeList = getApp().getSearchConditionedData().getTimeList();
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		dateAdapter = null;
		slotAdapter = null;
		timeAdapter = null;
		
		//日期
		dateAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, DateList);
		dateAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		//時段
		slotAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, SlotList);
		slotAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		//時間
		timeAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, TimeList);
		timeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		
		spinner1.setAdapter(dateAdapter);
		spinner2.setAdapter(slotAdapter);
		spinner3.setAdapter(timeAdapter);
	}

}
